package org.smile.pager.jsptags;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.smile.db.PageModel;
import org.smile.pager.Pager;
/**
 * 适用于jsptags分页标签的一个实现
 * @author strive
 *
 */
public class JspTagsPager implements Pager {
	/**
	 * 每页显示条数
	 */
	private int size=15;
	/**
	 * 当前页
	 */
	private int page=1;
	
	private String id="pager";
	
	private HttpServletRequest request=null;
	
	public JspTagsPager(HttpServletRequest request){
		this.request=request;
		refreshPage();
	}
	public JspTagsPager(HttpServletRequest request,int size){
		this.request=request;
		refreshPage();
		this.size=size;
	}
	/**
	 * 重新获取当前页
	 */
	public void refreshPage() {
		String offset=request.getParameter(id+"_offset");
		String size=request.getParameter(id+"_size");
		if(size!=null&&!"".equals(size)){
			this.size=Integer.parseInt(size);
		}
		if(offset!=null&&!"".equals(offset)){
			this.page=Integer.parseInt(offset)/this.size+1;
		}
		
	}
	public void setId(String id){
		int temp=this.size;
		this.id=id;
		refreshPage();
		this.size=temp;
	}
	public void commitResult(PageModel pageModel) {
		request.setAttribute("pageModel", pageModel);
	}
	public int getPage() {
		return page;
	}

	public HttpServletRequest getRequest() {
		return request;
	}
	public int getSize() {
		return size;
	}
	public Map getParameterMap() {
		return request.getParameterMap();
	}
}
